{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def mytest():\n",
    "    print('mytest function')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mytest function\n"
     ]
    }
   ],
   "source": [
    "mytest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my name is yasaka, my age is 19\n"
     ]
    }
   ],
   "source": [
    "def mytest(name, age):\n",
    "    print('my name is %s, my age is %d' % (name, age))\n",
    "    \n",
    "mytest('yasaka', 19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my name is yasaka, my age is 18\n",
      "my name is yasaka, my age is 28\n",
      "my name is yasaka, my age is 28\n",
      "my name is yasaka, my age is 28\n"
     ]
    }
   ],
   "source": [
    "def mytest(name, age=18):\n",
    "    print('my name is %s, my age is %d' % (name, age))\n",
    "    \n",
    "mytest('yasaka')\n",
    "mytest('yasaka', 28)\n",
    "mytest(name='yasaka', age=28)\n",
    "mytest(age=28, name='yasaka')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my name is yasaka, my age is 19\n",
      "() 0\n",
      "my name is yasaka, my age is 19\n",
      "('soccer', 'basketball', 'baseball') 3\n"
     ]
    }
   ],
   "source": [
    "def mytest(name, age, *hobby):\n",
    "    print('my name is %s, my age is %d' % (name, age))\n",
    "    print(hobby, len(hobby))\n",
    "    \n",
    "mytest('yasaka', 19)\n",
    "mytest('yasaka', 19, 'soccer', 'basketball', 'baseball')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my name is yasaka, my age is 19\n",
      "{} 0\n",
      "my name is yasaka, my age is 19\n",
      "{'fisrt_hobby': 'soccer', 'second_hobby': 'basketball', 'third_hobby': 'baseball'} 3\n"
     ]
    }
   ],
   "source": [
    "def mytest(name, age, **hobby):\n",
    "    print('my name is %s, my age is %d' % (name, age))\n",
    "    print(hobby, len(hobby))\n",
    "    \n",
    "mytest('yasaka', 19)\n",
    "mytest('yasaka', 19, fisrt_hobby='soccer', second_hobby='basketball', third_hobby='baseball')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55\n",
      "此方法是来将两个对象进行加和\n"
     ]
    }
   ],
   "source": [
    "# 此方法是来将两个对象进行加和\n",
    "def my_add(a, b):\n",
    "    \"\"\"此方法是来将两个对象进行加和\"\"\"\n",
    "    return a+b\n",
    "\n",
    "print(my_add(50, 5))\n",
    "print(my_add.__doc__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "150"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def func1(x):\n",
    "    y = 100\n",
    "    return x+y\n",
    "\n",
    "func1(50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'z' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-18-e0a3c1d18b6d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mz\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfunc1\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m50\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfunc2\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m30\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-18-e0a3c1d18b6d>\u001b[0m in \u001b[0;36mfunc2\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mfunc2\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mz\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfunc1\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m50\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfunc2\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m30\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'z' is not defined"
     ]
    }
   ],
   "source": [
    "def func1(x):\n",
    "    z = 100\n",
    "    return x+z\n",
    "\n",
    "def func2(x):\n",
    "    return x+z\n",
    "\n",
    "print(func1(50), func2(30))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "150 130\n"
     ]
    }
   ],
   "source": [
    "y = 100\n",
    "\n",
    "def func1(x):\n",
    "    return x+y\n",
    "\n",
    "def func2(x):\n",
    "    return x+y\n",
    "\n",
    "print(func1(50), func2(30))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "就近原则"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "150 230\n"
     ]
    }
   ],
   "source": [
    "y = 100\n",
    "\n",
    "def func1(x):\n",
    "    return x+y\n",
    "\n",
    "def func2(x):\n",
    "    y = 200\n",
    "    return x+y\n",
    "\n",
    "print(func1(50), func2(30))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "global关键字\n",
    "全局变量的访问或者说使用是直接可以去用的，不过要注意一下就近原则，才能确定是否真的访问到了你要的全局变量\n",
    "全局变量的重新赋值，我们必须在函数内部去使用global关键字才可以"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    }
   ],
   "source": [
    "y = 100\n",
    "\n",
    "def func():\n",
    "    global y\n",
    "    y = 200\n",
    "\n",
    "func()\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "递归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = list(range(1, 101))\n",
    "print(data)\n",
    "\n",
    "def my_add(a,b):\n",
    "    if len(a)==1:\n",
    "        return a[0]+b\n",
    "    return my_add(a[:-1] ,a[-1]) + b\n",
    "\n",
    "my_add(data[:-1], data[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[123, 45.67, -620000000.0, 123, 45.67, -620000000.0]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myseq = [123, 45.67, -6.2e8]\n",
    "\n",
    "def mytest(num):\n",
    "    return num*2\n",
    "\n",
    "mytest(10)\n",
    "mytest(myseq)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "所谓高阶函数，就是不光可以传递变量，还可以传递函数\n",
    "面向函数编程，说白了就是把函数传来传去，函数是第一要素\n",
    "面向对象编程，说白了就是把对象传来传去，对象是第一要素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换序列中的数值，要它们统一为一样的数值类型\n",
      "转换序列中的数值，要它们统一为一样的数值类型\n",
      "转换序列中的数值，要它们统一为一样的数值类型\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[246, 91.34, -1240000000.0]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myseq = [123, 45.67, -6.2e8]\n",
    "\n",
    "def convert(func, seq):\n",
    "    print('转换序列中的数值，要它们统一为一样的数值类型')\n",
    "    return [func(eachNum) for eachNum in seq]\n",
    "\n",
    "convert(int, myseq)\n",
    "convert(float, myseq)\n",
    "convert(mytest, myseq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[246, 91.34, -1240000000.0]"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(mytest, myseq))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[246, 91.34, -1240000000.0]"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(lambda x: x*2, myseq))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = list(range(1, 101))\n",
    "print(data)\n",
    "from functools import reduce\n",
    "\n",
    "def my_add(a,b):\n",
    "    return a+b\n",
    "\n",
    "reduce(my_add, data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = list(range(1, 101))\n",
    "print(data)\n",
    "from functools import reduce\n",
    "reduce(lambda a,b: a+b , data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]\n"
     ]
    }
   ],
   "source": [
    "def my_condition(x):\n",
    "    return x%2==0\n",
    "\n",
    "print(list(filter(my_condition, data)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]\n"
     ]
    }
   ],
   "source": [
    "print(list(filter(lambda x: x%2==0, data)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 6, -20, -70]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted([1,3,6,-20,-70],key=abs)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
